package com.yjd.comm.util;

import com.alibaba.fastjson.JSON;
import com.yjd.comm.zhima.model.ZhimaAuthApplyModel;
import com.yjd.comm.zhima.model.ZhimaCallBackModel;
import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 医美分期
 * Created by yjd-wap on 2017/8/21.
 */
public class ZhimaAuthUtils  {

    public static ZhimaAuthApplyModel getZhimaAuthModel(Long user_id, String idname, String idcard)throws Exception{
        ZhimaAuthApplyModel zhimaAuthApplyModel = new ZhimaAuthApplyModel();
        String authUrl = FrameUtil.getConstResource("zhima.auth.url").toString();
        String authKey = FrameUtil.getConstResource("zhima.auth.key").toString();
        if (StringUtils.isBlank(authUrl) || StringUtils.isBlank(authKey)) {
            FrameUtil.throwBaseException1("芝麻授权接口参数错误!");
        }

        if(user_id==null){
            FrameUtil.throwBaseException1("用户id不能为空");
        }

        if(StringUtil.isEmpty(idname)){
            FrameUtil.throwBaseException1("用户真实姓名不能为空");
        }

        if(StringUtil.isEmpty(idcard)){
            FrameUtil.throwBaseException1("用户证件号不能为空");
        }

        String keyStr = SecurityCode.md5(authKey);
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("user_id", user_id.toString());
        param.put("id_name" ,idname);
        param.put("id_card", idcard);

        zhimaAuthApplyModel.setAuthUrl(authUrl);
        zhimaAuthApplyModel.setApplyData(CryptAES.AES_Encrypt(keyStr, JSON.toJSONString(param)));
        return zhimaAuthApplyModel;
    }

    public static ZhimaCallBackModel parseZhimaCreditData(String app_msg)throws Exception{
        if(StringUtil.isEmpty(app_msg)){
            FrameUtil.throwBaseException1("请求参数不能为空");
        }
        String authKey = FrameUtil.getConstResource("zhima.auth.key").toString();
        String keyStr = SecurityCode.md5(authKey);
        String aesStr = CryptAES.AES_Decrypt(keyStr, app_msg);
        ZhimaCallBackModel  callBackModel = StringUtil.getObject(aesStr,ZhimaCallBackModel.class);
        return callBackModel;
    }
}
